package com.alan.controller;

import com.alan.entity.teach;
import com.alan.repository.AdminRepository;
import com.alan.repository.TeacherRepository;
import com.alan.repository.impl.AdminRepsitoryImpl;
import com.alan.repository.impl.TeacherRepositoryImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet("/adminteacher")
public class AdminTeacherServlet extends HttpServlet {
    private TeacherRepository teacherRepository = new TeacherRepositoryImpl();
    private AdminRepository adminRepository = new AdminRepsitoryImpl();


    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpSession session = req.getSession();
        String pagestr = req.getParameter("page");
        Integer page = Integer.parseInt(pagestr);
        session.setAttribute("page", page);
        String method = req.getParameter("method");
        String name = req.getParameter("name");
        String t_class = req.getParameter("t_class");
        switch (method) {
            case "update":
                teach teach = teacherRepository.find(name, t_class);
                req.setAttribute("teacher", teach);
                req.getRequestDispatcher("/admin/TeacherUpdate.jsp").forward(req, resp);
                break;
            case "delete":
                adminRepository.delete(name, t_class);
                resp.sendRedirect("/admin?method=teacher&page=" + page);
                break;
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpSession session = req.getSession();
        String method = req.getParameter("method");
        switch (method) {
            case "addteacher":
                String name = req.getParameter("name");
                String password = req.getParameter("password");
                String t_class = req.getParameter("t_class");
                String re = teacherRepository.registerfind(name);
                if (re == "success") {
                    req.setAttribute("info", "addteacher");
                    req.getRequestDispatcher("fail.jsp").forward(req, resp);
                } else {
                    teacherRepository.add(name, password, t_class);
                    resp.sendRedirect("/admin?method=teacher&page=1");
                }
                break;
            case "update":
                Integer page = (Integer) session.getAttribute("page");
                name = req.getParameter("name");
                password = req.getParameter("password");
                t_class = req.getParameter("t_class");
                adminRepository.update(name, password, t_class);
                resp.sendRedirect("/admin?method=teacher&page=" + page);
                break;

        }


    }
}
